#============================================================
#	Script Method List
#============================================================
# @datetime: 2022-5-3 20:16:36
#============================================================
class_name _CodeEditor_ScriptMethodDataList


class CustomSort:
	static func _sort(a, b) -> bool:
		return a['name'] < b['name']


##  解析脚本数据 
## @script  
## @return  
static func parse_script_data(script: Script) -> Dictionary:
	var data_list : Dictionary = {}
	var added : Dictionary = {}
	
	var data = ScriptUtil.get_script_method_data(script, true)
	for path in data:
		var method_data_list : Array = data[path]
		var curr_list = []
		data_list[path] = curr_list
		
		method_data_list.sort_custom(CustomSort, "_sort")
		
		for method_data in method_data_list:
			var method : String = method_data['name']
			if not added.has(method):
				added[method] = true
				curr_list.append({
					"name": method_data['name'],
					"args": _to_args(method_data['args']),
					"return": _to_return(method_data['return']),
				})
	return data_list


static func _to_args(list: Array) -> Array:
	if list.size() == 0:
		return []
	var l = []
	for i in list.size():
		l.append({
			"name": "_arg%d" % i,
			'type': _type_string(list[i]['type']),
		})
	return l


static func _to_return(data: Dictionary) -> String:
	if data['type'] != TYPE_NIL:
		return _type_string(data['type'])
	return ""



##  返回类型名称
static func _type_string(type: int) -> String:
	type = int(type)
	match type:
		TYPE_NIL:
			return ""
		TYPE_BOOL:
			return "bool"
		TYPE_INT:
			return "int"
		TYPE_REAL:
			return "float"
		TYPE_STRING:
			return "String"
		TYPE_VECTOR2:
			return "Vector2"
		TYPE_RECT2:
			return "Rect2"
		TYPE_VECTOR3:
			return "Vector3"
		TYPE_TRANSFORM2D:
			return "Transform2D"
		TYPE_PLANE:
			return "Plane"
		TYPE_QUAT:
			return "Quat"
		TYPE_AABB:
			return "AABB"
		TYPE_BASIS:
			return "Basis"
		TYPE_TRANSFORM:
			return "Transform"
		TYPE_COLOR:
			return "Color"
		TYPE_NODE_PATH:
			return "NodePath"
		TYPE_RID:
			return "RID"
#		TYPE_OBJECT:
#			return "Object"
		TYPE_DICTIONARY:
			return "Dictionary"
		TYPE_ARRAY \
		, TYPE_RAW_ARRAY \
		, TYPE_INT_ARRAY \
		, TYPE_REAL_ARRAY \
		, TYPE_STRING_ARRAY \
		, TYPE_VECTOR2_ARRAY \
		, TYPE_VECTOR3_ARRAY \
		, TYPE_COLOR_ARRAY:
			return "Array"
		_:
			return ""


